package com.education.wisdom.controller.app;


import com.education.common.core.BaseController;
import com.education.common.utils.BusinessType;
import com.education.common.utils.LogUtil;
import com.education.common.utils.Token;
import com.education.wisdom.pojo.JrLogininfor;
import com.education.wisdom.pojo.SysAppLogin;
import com.education.wisdom.service.IJrLogininforService;
import com.education.wisdom.service.ISysAppLoginService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 闫清
 * @date : Created in 17:51 2018/10/24
 */
@Controller
@CrossOrigin
@Slf4j
@RequestMapping("/appIndex")
public class AppIndexController extends BaseController {

    @Autowired
    private ISysAppLoginService SysAppLoginService;
//    @Autowired
//    private AuthenticationManager authenticationManager;
    @Autowired
    private IJrLogininforService jrLogininforService;
//    /**
//     * 登录
//     *
//     * @param username 用户名
//     * @param password 密码
//     * @return 结果
//     */
//    @PostMapping("/checkLogin")
//    @ResponseBody
//    @ApiOperation("登录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "登录账号", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "String")})
//    public void checkLogin(HttpServletResponse response,HttpServletRequest request, HttpSession session, String username, String password) throws Exception {
//        log.info(LogUtil.LogInfo("登录", new Date()));
//        Map<String, Object> map = new HashMap<>();
//        map.put("username", username);
//        map.put("password", Token.encoderByMd5(password));
//        SysAppLogin user = SysAppLoginService.selectSysAppLogin(map);
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        if (user != null) {
//            if (user.getUserState() == 0) {
//                Integer logins=user.getLogins();
//                JrLogininfor logininfor=new JrLogininfor();
//                logininfor.setLoginName(username);
//                List<JrLogininfor> loginfor=jrLogininforService.selectLogininforList(logininfor);
//                int sum=loginfor.size();
//                if(sum<logins){
//                    JrLogininfor lfor=new JrLogininfor();
//                    lfor.setLoginName(username);
//                    lfor.setLoginTime(new Date());
//                    jrLogininforService.insertLogininfor(lfor);
//                JwtAuthenticatioToken token = SecurityUtils.login(request, username, password, authenticationManager);
//                user.setToken(token.getToken());
//                user.setUserPassword(null);
//                session.setAttribute("user", user);
//                writeJSON(user, response, Constant.LOGIN_SUCCESS, "true");
//                }else{
//                    writeJSON("您的账号超过登录次数！", response, Constant.LOGIN_FAILURE, "true");
//                }
//            } else if (user.getUserState() == 1) {
//                writeJSON("您的账号异常，请及时联系管理员！", response, Constant.LOGIN_FAILURE, "true");
//            } else if (user.getUserState() == 2) {
//                writeJSON("您的账号被加入黑名单，请及时联系管理员！", response, Constant.LOGIN_FAILURE, "true");
//            }
//        } else {
//            writeJSON("用户名或密码输入错误", response, Constant.LOGIN_FAILURE, "用户名或密码输入错误");
//        }
//    }


    /**
     * 判断用户是否参在
     *
     * @param
     * @param
     * @return 结果
     */
    @RequestMapping("/isThereAre")
    @ResponseBody
    @ApiOperation("判断用户是否存在")
    @ApiImplicitParams({@ApiImplicitParam(name = "userPhone", value = "登录账号", required = true, dataType = "String")})
    public void isThereAre(HttpServletResponse response, String userPhone) throws Exception {
        log.info(LogUtil.LogInfo("判断用户是否存在", new Date()));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userPhone", userPhone);
        SysAppLogin AppLogin = SysAppLoginService.selectByMap(map);
        if (AppLogin != null) {
            writeJSON("用户存在", response, "200", "success");
        } else {
            writeJSON("用户名输入有误！", response, "201", "success");
        }
    }


    /**
     * 修改密码
     *
     * @param
     * @param password 密码
     * @return 结果
     */
    @RequestMapping("/changePassword")
    @ResponseBody
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone", value = "登录账号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "String")})
    public void changePassword(HttpServletResponse response, String userPhone, String password) throws Exception {
        log.info(LogUtil.LogInfo("修改密码", new Date()));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userPhone", userPhone);
        SysAppLogin AppLogin = SysAppLoginService.selectByMap(map);
        AppLogin.setUserPassword(Token.encoderByMd5(password));
        writeJSON(SysAppLoginService.updateSysAppLoginbyuserPhone(AppLogin), response, "201", "success");
    }

    /**
     * 上传头像
     *
     * @return 结果
     */
    @RequestMapping("/checkIco")
    @ResponseBody
    @ApiOperation("修改个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "登录用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userIco", value = "用户头像", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userAccount", value = "用户姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userAddress", value = "联系地址", required = false, dataType = "String")})
    public void checkIco(HttpServletResponse response, HttpServletRequest request, SysAppLogin sysAppLogin) throws Exception {
        log.info(LogUtil.LogInfo("修改个人信息", new Date()));
        int updateSysAppLogin = SysAppLoginService.updateSysAppLogin(sysAppLogin);
        writeJSON(updateSysAppLogin, response, "200", "success");
    }


}